from django.core.management.base import BaseCommand
from apps.flowers.models import FlowerCategory

class Command(BaseCommand):
    help = '更新分类图片为OSS链接'

    def handle(self, *args, **options):
        # 分类图片映射
        category_images = {
            "玫瑰花": "https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/红玫瑰花束.jpg",
            "康乃馨": "https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/红康乃馨花束.jpg", 
            "百合花": "https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/白百合花束.jpg",
            "向日葵": "https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/向日葵花束.jpg",
            "郁金香": "https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/粉郁金香花束.jpg",
            "满天星": "https://flower-1.oss-cn-shanghai.aliyuncs.com/flowers/满天星花束.jpg"
        }
        
        success_count = 0
        
        for category_name, image_url in category_images.items():
            try:
                category = FlowerCategory.objects.filter(name=category_name).first()
                if category:
                    category.image = image_url
                    category.save()
                    self.stdout.write(f"✓ 更新分类图片: {category_name}")
                    success_count += 1
                else:
                    self.stdout.write(f"✗ 未找到分类: {category_name}")
            except Exception as e:
                self.stdout.write(f"✗ 更新失败 {category_name}: {e}")
        
        self.stdout.write(f"\n更新完成！成功: {success_count}")